package com.ruoyi.web.controller.checksystem;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.checksystem.domain.Userinfo;
import com.ruoyi.checksystem.service.IUserinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/checksystem/userinfo")
public class UserinfoController extends BaseController
{
    @Autowired
    private IUserinfoService userinfoService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Userinfo userinfo)
    {
        startPage();
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Userinfo userinfo)
    {
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        ExcelUtil<Userinfo> util = new ExcelUtil<Userinfo>(Userinfo.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userinfoService.selectUserinfoById(id));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Userinfo userinfo)
    {
        return toAjax(userinfoService.insertUserinfo(userinfo));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Userinfo userinfo)
    {
        return toAjax(userinfoService.updateUserinfo(userinfo));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('checksystem:userinfo:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userinfoService.deleteUserinfoByIds(ids));
    }

    /**
     * 验证用户登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Userinfo userinfo)
    {
        // 1. 根据手机号查询用户
        Userinfo query = new Userinfo();
        query.setPhone(userinfo.getPhone());
        List<Userinfo> users = userinfoService.selectUserinfoList(query);
        
        if (users == null || users.isEmpty()) {
            return AjaxResult.error("用户不存在");
        }
        
        // 2. 验证密码
        Userinfo dbUser = users.get(0);
        if (userinfoService.validatePassword(userinfo.getPassword(), dbUser.getPassword())) {
            // 登录成功，返回用户信息（不包含密码）
            dbUser.setPassword(null);
            return AjaxResult.success("登录成功", dbUser);
        } else {
            return AjaxResult.error("密码错误");
        }
    }
}
